/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bkt.controller;

import bkt.DataFormat;
import bkt.app.facade.Facade;
import bkt.model.hr.attendance.Attendance;
import bkt.model.person.Employee;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author hashcode
 */
@WebServlet(name = "servletController", urlPatterns = {"/employee", "/employeelist", "/newemployee", "/index", "/hrmenu", "/attendancemenu",
    "/checkin", "/checkout", "/reportcheckin", "/submitnewemployee", "/leavecontrol", "/leaveapplication"})
public class servletController extends HttpServlet {

    //private List<Employee> employeeList = new ArrayList<Employee>();
    private Facade data = new Facade();
    private DataFormat dataFormat = new DataFormat();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet servletController</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet servletController at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userPath = request.getServletPath();

        // if login page is requested

         if (userPath.equals("/reportcheckin")) {

            userPath = "/reportcheckin";
        }

        if (userPath.equals("/reportcheckin")) {

            userPath = "/reportcheckin";
        }

        if (userPath.equals("/employee")) {

            userPath = "/employee";
        }
          if (userPath.equals("/leavecontrol")) {

            userPath = "/leavecontrol";
        }
             if (userPath.equals("/leaveapplication")) {

            userPath = "/leaveapplication";
        }
        

        if (userPath.equals("/submitnewemployee")) {
            //userPath = "/employeelist";
            System.out.println("Bruno check");
            String name, surName, eMail, password;
            name = (String) request.getParameter("txtName");
            eMail = (String) request.getParameter("txtEmail");
            surName = (String) request.getParameter("txtSurname");
            password = (String) request.getParameter("txtPassword");
            String employeenumber = "" + (data.getEmployeeService().count() + 1);

            Employee employee = new Employee();
            employee.setEmpEmail(eMail);
            employee.setEmpName(name);
            employee.setEmpSurname(surName);
            employee.setEmpPassword(password);
            employee.setEmpNumber(employeenumber);
            System.out.println("numbEmployee herre");
            if (name != null) {
                data.getAppEmployeeService().saveNewEmployee(employee);
                //request.setAttribute("txtName", null);
                //name = null;
                //request = null;
            }

            System.out.println("employee password is : " + password);
            List<Employee> listEmployee = data.getEmployeeService().findAll();

            long numbEmployee = data.getEmployeeService().count();
            request.setAttribute("Employeelist", listEmployee);
            request.setAttribute("employeecount", numbEmployee);
            System.out.println("numbEmployee count " + numbEmployee);
            for (Employee employee1 : listEmployee) {
                System.out.println("employee name: " + employee1.getEmpName());
            }
            userPath = "/employeelist";

        }
        if(userPath.equals("/employeelist")){
            userPath = "/employeelist"; 
            List<Employee> listEmployee = data.getEmployeeService().findAll();
            request.setAttribute("Employeelist", listEmployee);
        }

        if (userPath.equals("/index")) {

            userPath = "/index";
        }
        if (userPath.equals("/hrmenu")) {

            userPath = "/hrmenu";
        }
        if (userPath.equals("/attendancemenu")) {

            userPath = "/attendancemenu";
        }
        if (userPath.equals("/checkin")) {

            userPath = "/checkin";

        }
        // use RequestDispatcher to forward request internally
        System.out.println("RRRRR:   " + userPath);
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            //request.setAttribute("LINK_ME",request.getAttribute("action"));      
            request.getRequestDispatcher(url).forward(request, response);

        } catch (Exception ex) {
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userPath = request.getServletPath();

        // if login page is requested
        if (userPath.equals("/reportcheckin")) {

            userPath = "/reportcheckin";
            String strDateStart, strDateEnd, strEmpNumber;
            strDateStart = (String) request.getParameter("txtDateStart");
            strDateEnd = (String) request.getParameter("txtDateEnd");
            strEmpNumber = (String) request.getParameter("txtEmpNumber");

            System.out.println("Calender Date IN: " + strDateStart);
            System.out.println("Calender Date OUT: " + strDateEnd);

            Date datStartDate, datEndDate;

            datStartDate = new Date(strDateStart);
            datEndDate = new Date(strDateEnd);

            Attendance attendance = data.getCheckinAndOutReportService().checkinCheckoutReport(strEmpNumber, datStartDate, datEndDate);

            System.out.println("servlet All Size checkin:  " + attendance.getCheckins().size());
            System.out.println("servlet All Size checkout:  " + attendance.getCheckouts().size());

            request.setAttribute("checkinList", attendance.getCheckins());
            request.setAttribute("checkoutList", attendance.getCheckouts());
            request.setAttribute("strDateStart", strDateStart);
            request.setAttribute("strDateEnd", strDateEnd);
            request.setAttribute("strEmpNumber", strEmpNumber);


        }
        if (userPath.equals("/employee")) {

            userPath = "/employee";
        }
        if (userPath.equals("/checkin")) {

            userPath = "/checkin";
            String strEmpNumber = null;
            strEmpNumber = (String) request.getParameter("txtEmpNumber");
            if (strEmpNumber != null) {
                String strCheckinFeedback = data.getCheckinAndOutReportService().CheckinByEmpNumber(strEmpNumber);
                request.setAttribute("strCheckinFeedback", strCheckinFeedback);
            }
        }
        if (userPath.equals("/checkout")) {

            userPath = "/checkout";
            String strEmpNumber = null;
            strEmpNumber = (String) request.getParameter("txtEmpNumber");
            if (strEmpNumber != null) {
                String strCheckoutFeedback = data.getCheckinAndOutReportService().CheckoutByEmpNumber(strEmpNumber);
                request.setAttribute("strCheckoutFeedback", strCheckoutFeedback);
            }
        }
        // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            //request.setAttribute("LINK_ME",request.getAttribute("action"));      
            request.getRequestDispatcher(url).forward(request, response);

        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
